package com.example.control;


import com.example.domain.t_article;
import com.example.domain.t_user;
import com.example.service.IArticleService;
import com.example.service.ISiteService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RequestMapping("/admin")
@Controller
public class AdminCateControl {
    private static final Logger logger= (Logger) LoggerFactory.getLogger(CommentControl.class);

    @Autowired
    private IArticleService art;
    @Autowired
    private ISiteService site;

    //主页面
    @RequestMapping({"/category","/cate","/tags"})
    public String getindex(HttpServletRequest request){
        //System.out.println(count+","+size);
        t_user loginUser = (t_user)request.getSession().getAttribute("loginUser");

        //封装要进行的分类查找
        t_article likeArt = new t_article();

        likeArt.setUser_id(loginUser.getId());
        //得到所有的标签
        Set<String> tagslist= new HashSet<String>();
        List<t_article> t_articles = art.selectList(loginUser.getId());
        for(t_article t:t_articles){
            if(t.getTags()==null||"".equals(t.getTags()))continue;
            String[] split = t.getTags().split(",");
            //System.out.println(split.length);

            tagslist.addAll(Arrays.asList(split));
        }
        tagslist.remove("");
        tagslist.remove(" ");
        request.setAttribute("tagsList",tagslist);
        //System.out.println(tagslist);
        //按照article详细分类
        List<String> categories = art.getCategories(loginUser.getId());
        request.setAttribute("categoriesList",categories);

        return "back/class";
    }

    //修改标签或分类
    @RequestMapping("/setCate")
    @ResponseBody
    public boolean setCate(HttpServletRequest request,String old,String news,boolean type){
        t_user loginUser = (t_user) request.getSession().getAttribute("loginUser");

        if(loginUser==null || old==null||news==null){
            return false;
        }


        if(type){
            logger.info("分类管理,修改标签 "+old+" by "+loginUser.getUsername());
            return art.updateTagsByUser_id(old,news,loginUser.getId());
        }else{
            logger.info("分类管理,修改分类 "+old+" by "+loginUser.getUsername());
            return art.updateCateByUser_id(old,news,loginUser.getId());
        }



    }
    //删除标签或分类
    @RequestMapping("/delCate")
    @ResponseBody
    public boolean delCate(HttpServletRequest request,String old,boolean type){
        t_user loginUser = (t_user) request.getSession().getAttribute("loginUser");

        if(loginUser==null){
            return false;
        }
        t_article article = new t_article();
        article.setUser_id(loginUser.getId());
        if(type){
            logger.info("分类管理,删除标签 "+old+" by "+loginUser.getUsername());
            art.updateTagsByUser_id(old,"",loginUser.getId());
        }else{
            logger.info("分类管理,删除分类 "+old+" by "+loginUser.getUsername());
            art.updateCateByUser_id(old,"",loginUser.getId());
        }

        return true;

    }

}
